<?php

namespace App\Http\Controllers\Admin\SystemSet\BlackList;

use App\Exceptions\BasicException;
use App\Http\Controllers\BaseController;
use Illuminate\Http\Request;    //引用http请求类
use App\Response\Response;

class BlackListUserController extends BaseController {

    /**
     * @api {post} /system-set/black-list/user 黑名单用户创建
     * @apiVersion 0.1.0
     * @apiName 系统设置-黑名单用户创建
     * @apiGroup 系统设置
     *
     *
     * @apiParam {number} user_id                   用户id
     * @apiParam {array} cate_ids                   黑名单类别
     *
     * @apiSuccessExample 成功响应
     *
     *      {"code":200,"data":true}
     *
     * @apiError 10001 参数校验失败
     *
     * @apiErrorExample 错误响应
     *
     *      {
     *          "code": 10001,
     *          "message": "errorMsg"
     *      }
     *
     */
    public function create(Request $request)
    {
        //参数校验
        $v = app('validator')->make($request->all(),
            [
                'user_id' => 'required|numeric|exists:mysql.ln_user,user_id',
                'cate_ids' => 'required|array',
            ],
            [
                'user_id.required' => '用户ID不能为空',
                'user_id.numeric' => '用户ID必须为数字',
                'user_id.exists' => '用户ID不存在',
            ]);
        if ($v->fails()) throw new BasicException(10001,$v->errors()->first());
        $params = $request->input();

        $data = app('repo_black_list')->getUser(["user_id" => $params['user_id']]);
        if($data){
            throw new BasicException(10001, '该用户已存在黑名单！');
        }
        $cateIdsArr = $params['cate_ids'];
        $params['cate_ids'] = implode(",", $cateIdsArr);

        $userInfo = app('repo_user')->getUserInfo($params['user_id']);
        $params['user_code'] = $userInfo['user_no'];
        $params['nickname'] = $userInfo['user_data']['nickname'];
        $params['head_picture'] = $userInfo['user_data']['head_picture'];
        $params['mobile'] = $userInfo['mobile'];
        $params['type'] = 2;
        $params['add_time'] = time();
        $params['add_date_time'] = date("Y-m-d H:i:s");

        $res = app('repo_black_list');
        $rows = $res->addUser($params);
        if (!$rows) {
            throw new BasicException(10001, "插入失败！");
        }

        return Response::sendData(true);
    }

    /**
     * @api {delete} /system-set/black-list/user 黑名单用户删除
     * @apiVersion 0.1.0
     * @apiName 系统设置-黑名单用户删除
     * @apiGroup 系统设置
     *
     * @apiParam {string} ids                       字符串分割的id 必传
     *
     * @apiSuccessExample 成功响应
     *
     *      {"code":200,"data":true}
     *
     * @apiError 10001 参数校验失败
     *
     * @apiErrorExample 错误响应
     *
     *      {
     *          "code": 10001,
     *          "message": "errorMsg"
     *      }
     *
     */
    public function del(Request $request) {
        //参数校验
        try {
            $this->validate($request, [
                'ids' => 'required|string',
            ]);

        } catch (\Exception $e) {
            throw new BasicException(10001, json_encode($e->errors()));
        }

        $idsStr = $request->input("ids");
        $idsArr = explode("," , $idsStr);

        $rows = app('repo_black_list')->delUser($idsArr);
        if (!$rows) {
            throw new BasicException(10001, "删除失败！");
        }

        return Response::sendData(true);
    }

    /**
     * @api {put} /system-set/black-list/user 黑名单用户修改
     * @apiVersion 0.1.0
     * @apiName 系统设置-黑名单用户修改
     * @apiGroup 系统设置
     *
     *
     * @apiParam {int} id
     * @apiParam {array} cate_ids                   黑名单类别
     *
     * @apiSuccessExample 成功响应
     *
     *      {"code":200,"data":true}
     *
     * @apiError 10001 参数校验失败
     *
     * @apiErrorExample 错误响应
     *
     *      {
     *          "code": 10001,
     *          "message": "errorMsg"
     *      }
     *
     */
    public function update(Request $request) {
        //参数校验
        try {
            $this->validate($request, [
                'id' => 'required|integer',
                'cate_ids' => 'required|array',
            ]);
        } catch (\Exception $e) {
            throw new BasicException(10001, json_encode($e->errors()));
        }

        $id = $request->input('id');
        $res = app('repo_black_list');

        $params = [];

        $cateIdsArr = $request->input('cate_ids');
        $params['cate_ids'] = implode(",", $cateIdsArr);

        $params['add_time'] = time();
        $params['add_date_time'] = date("Y-m-d H:i:s");

        $rows = $res->saveUser(["id" => $id], $params);
        if (!$rows) {
            throw new BasicException(10001, "更新失败！");
        }

        return Response::sendData(true);
    }

    /**
     * @api {get} /system-set/black-list/user 黑名单用户列表
     * @apiVersion 0.1.0
     * @apiName 系统设置-黑名单用户列表
     * @apiGroup 系统设置
     *
     * @apiParam {number} page 当前页数 可选
     * @apiParam {number} perPage 分页容量 可选
     *
     *
     * @apiSuccessExample 成功响应
     *
     *
    {
        "code": 200,
        "data": {
            "list": [
                {
                    "id": 19,
                    "cate_ids(黑名单类别)": "5,6",
                    "user_id(用户id)": 370000,
                    "user_code(用户编号)": 250230,
                    "nickname(用户昵称)": "asdasd",
                    "head_picture(用户头像地址)": "asdasd",
                    "mobile(用户手机号)": "123333333",
                    "mobile_belong_to(用户手机归属地)": "山东",
                    "ip(用户ip)": "114.114.114.114",
                    "type(添加方式 1 自动  2手动)": 1,
                    "add_date_time(受限时间)": "asdasd"
                }
            ],
            "meta": {
                "page": 1,
                "limit": 10,
                "next": null,
                "total": 1
            }
        },
        "header": {
            "type_list(黑名单类别列表)": [
                3,
                4,
                5,
                6,
                111
            ]
        }
    }
     *
     *
     */
    public function list(Request $request) {
        //参数校验
        try {
            $this->validate($request, [
                'perPage' => 'filled|numeric',
                'page' => 'filled|numeric',
            ]);
        } catch (\Exception $e) {
            throw new BasicException(10001, json_encode($e->errors()));
        }

        $list = app('repo_black_list')->getUserQueryList($request->input());

        $ext = [
            'type_list' => app('repo_black_list')->getTypeList()
        ];

        return Response::sendList($list,[], $ext);
    }


}
